package com.pine.controller;

import com.pine.pojo.Resume;
import com.pine.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/login")
    public ModelAndView login(HttpSession session, String username, String password,ModelAndView model) {
        System.out.println("登录开始————————————");

        model.setViewName("login");

        if ("admin".equals(username) && "admin".equals(password)) {
            session.setAttribute("username", username);
            session.setAttribute("password", password);

            List<Resume> resumes = resumeService.findAll();
            model.addObject("resumes", resumes);
            model.setViewName("list");

        } else if (null != username && null != password) {
            model.addObject("msg", "用户名或密码错误");
        }
        return model;
    }

    @RequestMapping("/without")
    public ModelAndView loginOut(HttpSession session) {
        System.out.println("......loginOut()");
        session.invalidate();
        return new ModelAndView("login");
    }
}
